package com.ptszyxx.popose.common.utils;


import android.content.Context;

import com.ptszyxx.popose.common.constants.AppConstant;
import com.ysg.http.data.entity.login.LoginResult;
import com.ptszyxx.popose.module.chat.entity.UserInfo;
import com.ptszyxx.popose.module.main.MainActivity;
import com.ysg.base.BaseViewModel;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;

public class YLoginUtil {
    private static YLoginUtil instance;

    private YLoginUtil(){
    }

    public static YLoginUtil getInstance(){
        if (instance == null){
            synchronized (YLoginUtil.class){
                if (instance == null){
                    instance = new YLoginUtil();
                }
            }
        }
        return instance;
    }


    public void login(BaseViewModel viewModel, LoginResult result){
        YSPUtils.getInstance().saveToken(result.getToken());//保存token
        if (result.getUser() != null){
            YSPUtils.getInstance().saveUserId(result.getUser().getId().toString());
            YSPUtils.getInstance().saveUserName(result.getUser().getNick());
            YSPUtils.getInstance().saveUserPhone(result.getUser().getLoginname());
            YSPUtils.getInstance().saveUserCode(result.getUser().getUsercode());
            YSPUtils.getInstance().saveUserPic(result.getUser().getPic());
            YSPUtils.getInstance().saveUserSex(result.getUser().getSex()+"");
            YSPUtils.getInstance().saveTxCode(result.getUser().getTengxuncode());
        }

        //iswanshan:2 跳到完善用户信息界面
        if (YStringUtil.eq(2, result.getUser().getIswanshan())) {
            YActivityUtil.getInstance().jumpUserNick(viewModel);
        } else {
            YSPUtils.getInstance().saveLogin(true);
            YActivityUtil.getInstance().jumpMainActivity(viewModel, null);
        }
        YActivityUtil.getInstance().close(viewModel);
        YSPUtils.getInstance().put(AppConstant.PRIVACY_AGREE, true);
    }

    public void logout(Context context){
        try {
            //腾讯IM退出
            UserInfo.getInstance().cleanUserInfo();
            YChatUtil.getInstance().logout();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //保存token
            YSPUtils.getInstance().saveLogin(false);
            YSPUtils.getInstance().saveToken("");
            //退到登录页
            MainActivity.finishMainActivity();
            YActivityUtil.getInstance().jumpLoginQuickActivity(context);
        }
    }


}
